<?php

namespace common\models;

use app\models\AdminLog;
use Yii;

/**
 * This is the model class for table "message".
 *
 * @property string $id
 * @property string $user_id
 * @property string $title
 * @property string $content
 * @property string $telephone
 * @property string $email
 * @property string $created_at
 * @property string $ip
 */
class Message extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'message';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['content'], 'required'],
            [['user_id', 'created_at'], 'integer'],
            [['content'], 'string'],
            [['title'], 'string', 'max' => 255],
            [['ip'], 'string', 'max' => 15],
            [['telephone', 'email'], 'string', 'max' => 64],
            ['email', 'email'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'user_id' => '留言用户id',
            'title' => '留言标题',
            'content' => '留言内容',
            'telephone' => '留言人手机',
            'email' => '留言人邮箱',
            'created_at' => '留言时间',
            'ip' => 'IP',
        ];
    }

    public function beforeSave($insert)
    {
        if ($this->isNewRecord) {
            $this->created_at = time();
            $this->ip = Yii::$app->request->userIP;
        }
        return parent::beforeSave($insert);
    }

    public function afterDelete()
    {
        parent::afterDelete();
        AdminLog::log("删除留言：" . $this->title, "delete");
    }
}
